# Maintainer: Renato Silva <br.renatosilva@gmail.com>

_realname='nsis'
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.05
pkgrel=1
url='https://nsis.sourceforge.io/'
pkgdesc='Windows installer development tool (mingw-w64)'
license=(ZLIB)
arch=(any)
depends=(${MINGW_PACKAGE_PREFIX}-zlib
         ${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-libwinpthread)
makedepends=(${MINGW_PACKAGE_PREFIX}-gcc
             'dos2unix'
             'scons>=2.3.4-3'
             rsync)
source=("https://sourceforge.net/projects/nsis/files/NSIS%203/${pkgver}/${_realname}-${pkgver}-src.tar.bz2"
        001-fhs-directory-structure.patch
        002-compile-fixes.patch
        003-remove-redundant-architecture-suffix.patch
        004-fix-jump-to-label-error.patch
        005-fix-call-to-non-constexpr-function.patch
        py3-fixes.patch)
sha256sums=('b6e1b309ab907086c6797618ab2879cb95387ec144dab36656b0b5fb77e97ce9'
            'a2719915676106bc0c545fca6d5871c96044c7e4ba8c95ea5ca03a0a49b2b6e3'
            '4b94e5f5979ea13e19a37606ec5861dfdc0004b9e4eaf984e4aae4a7fe5e85b7'
            'f43af517d7821084335a38bce15c7644cc114c71cf6f8ab860aa4d9322795343'
            '44d854b1d54befeb381ec8712a8baf624d08b014bdf1f1da18a8af2f77d4b8f4'
            'c07e7c13ebcfe38495dfdb886f2cea363e5a8a1c17bd786ece0f15625ac3f864'
            '5362830d73756534cd9c31f93214a7d529cee6e9291998fd5d98d6b887d40023')

# Circumvent problem where makepkg will add the exe extension to some files
# when compressing the 64-bit package
options=('!strip')

consolidate() {
  msg2 'Converting to unix line breaks'
  find -name 'SConstruct' | xargs dos2unix --quiet
  find -name 'SConscript' | xargs dos2unix --quiet
  find -name '*.[ch]'     | xargs dos2unix --quiet
  find -name '*.cpp'      | xargs dos2unix --quiet
  find -name '*.py'       | xargs dos2unix --quiet
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}-src"
  consolidate
  patch -p1 -i "${srcdir}"/001-fhs-directory-structure.patch
  patch -p1 -i "${srcdir}"/002-compile-fixes.patch
  patch -p1 -i "${srcdir}"/003-remove-redundant-architecture-suffix.patch
  #patch -p1 -i "${srcdir}"/004-fix-jump-to-label-error.patch
  patch -p1 -i "${srcdir}"/005-fix-call-to-non-constexpr-function.patch
  patch -p1 -i "${srcdir}"/py3-fixes.patch
}

_build() {
  local target_arch="${CARCH/i686/x86}"
  target_arch="${target_arch/x86_64/amd64}"

  local major_version="${pkgver%%.*}"
  local minor_version="${pkgver#*.}"
  minor_version="${minor_version%%[a-z]*}"
  msg2 "Major version defined as ${major_version}"
  msg2 "Minor version defined as ${minor_version}"

  cd "${srcdir}/build-${CARCH}-${_realname}-${pkgver}"
  scons \
      TARGET_ARCH="${target_arch}" \
      PREFIX="${MINGW_PREFIX}" \
      VERSION="${pkgver}" \
      VER_MAJOR="${major_version}" \
      VER_MINOR="${minor_version}" \
      UNICODE='yes' \
      NSIS_MAX_STRLEN='8192' \
      NSIS_CONFIG_CONST_DATA_PATH='no' \
      SKIPUTILS='NSIS Menu,SubStart' \
      PREFIX_DEST="${PREFIX_DEST}" \
      NSIS_CONFIG_LOG='yes' \
      ${target}
}

build() {
  msg2 'Synchronizing build directory'
  rsync --recursive --times "${srcdir}/${_realname}-${pkgver}-src"/* "${srcdir}/build-${CARCH}-${_realname}-${pkgver}"
  _build
}

package() {
  PREFIX_DEST="${pkgdir}" target='install' _build
  install -Dm644 "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  rm "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING"
}
